package com.zrf.testchatgpt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zrf.testchatgpt.entity.Permission;
import com.zrf.testchatgpt.entity.Role;
import com.zrf.testchatgpt.mapper.PermissionMapper;
import com.zrf.testchatgpt.service.impl.PermissionServiceImpl;
import com.zrf.testchatgpt.util.Result;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("auth")
@RequiresRoles("admin")
public class PermissionController {

    @Autowired
   private PermissionServiceImpl permissionService;
    @Autowired
    private PermissionMapper permissionMapper;

    @GetMapping("getPermissionByRoleId")
    public Result getPermissionByRoleId(String roleId){
        List<Integer> list = permissionService.getPermissionByRoleId(roleId);
        return Result.builder().code("200").data(list).build();
    }

    @GetMapping("getPermissionByUserId")
    public Result getPermissionByUserId(String userId){
        List<Integer> list = permissionService.getPermissionByUserId(userId);
        return Result.builder().code("200").data(list).build();
    }

    @GetMapping("queryByname")
    public Result queryByname(String name){
        LambdaQueryWrapper<Permission> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(Permission::getPermissionName,name);
        List<Permission> permissions = permissionMapper.selectList(wrapper);
        return Result.builder().code("200").data(permissions).build();
    }
    @PutMapping("updatePer/{id}")
    public Result updateRole(@PathVariable("id")String permissionId,@RequestBody Permission permission){
        int i = permissionMapper.updateById(permission);
        if (i>0)
        return Result.builder().code("200").msg("权限更新成功").build();
        else
            return Result.builder().code("500").msg("权限更新失败").build();
    }
    @PostMapping("addPer")
    public Result addRole(@RequestBody Permission permission){
        if (permission==null){
            return Result.builder().code("500").msg("添加失败").build();
        }
        permissionMapper.Myinsert(permission);
        return Result.builder().code("200").msg("添加成功").data(permission.getId()).build();
    }
    @DeleteMapping("deletePer/{id}")
    public Result deleteRole(@PathVariable("id")String permissionId){
        LambdaQueryWrapper<Permission> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Permission::getId,permissionId);
        permissionMapper.delete(wrapper);
        return Result.builder().code("200").msg("删除成功").build();
    }


}
